package com.djl.seckilldemo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.djl.seckilldemo.exception.GlobalException;
import com.djl.seckilldemo.mapper.UserMapper;
import com.djl.seckilldemo.pojo.User;
import com.djl.seckilldemo.service.IUserService;
import com.djl.seckilldemo.util.CookieUtil;
import com.djl.seckilldemo.util.MD5Util;
import com.djl.seckilldemo.util.UUIDUtil;
import com.djl.seckilldemo.vo.LoginVo;
import com.djl.seckilldemo.vo.RespBean;
import com.djl.seckilldemo.vo.RespBeanEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-10-14
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private  UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 登录逻辑
     * @param loginVo
     * @return
     */

    @Override
    public RespBean doLogin(LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {
        String mobile=loginVo.getMobile();
        String password=loginVo.getPassword();

        User user=userMapper.selectById(mobile);
        if (user==null){
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }

        if (!MD5Util.formPassToDBPass(password,user.getSalt()).equals(user.getPassword())){
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }
        //生成cooike
        String ticket= UUIDUtil.uuid();
        //request.getSession().setAttribute(ticket,user);
        //Cookie cookie=new Cookie("ticket",ticket);
        //将用户信息存入用户中
        redisTemplate.opsForValue().set("user:"+ticket,user);
        CookieUtil.setCookie(request,response,"ticket", ticket);

        return RespBean.success(ticket,RespBeanEnum.SUCCESS);
    }

    @Override
    public User getUserByCookie(String userTicket, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isEmpty(userTicket)){
            return null;
        }
        User user = (User) redisTemplate.opsForValue().get("user:" + userTicket);
        if (user==null){
            String ticket= UUIDUtil.uuid();
            CookieUtil.setCookie(request,response,"user:"+ticket,userTicket);
        }
        return user;
    }

    /**
     * 更新密码
     * @param ticket
     * @param password
     * @param request
     * @param response
     * @return
     */
    @Override
    public RespBean updatePassword(String ticket, String password,HttpServletRequest request, HttpServletResponse response) {
        User user = getUserByCookie(ticket, request, response);
        if (user==null){
            throw new GlobalException(RespBeanEnum.MOBILE_NOT_EXIT);
        }
        user.setPassword(MD5Util.inputPassToDbPass(password,user.getSalt()));
        int result = userMapper.updateById(user);
        if (1==result){
            //删除redis
            redisTemplate.delete("user:"+ticket);
            return RespBean.success();
        }
        return RespBean.error(RespBeanEnum.PASSWORD_UPDATE_FAIL);
    }
}
